﻿using Candy.Infrastructure;
using Candy.Model;
using Candy.Web.Data;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Candy.ViewModel
{
    public class MoveToFamilyViewModel : CandyViewModelBase
    {

        private MoveMode _moveMode = MoveMode.Family;

        private string _filter;
        public string Filter
        {
            get
            {
                return _filter;
            }
            set
            {
                _filter = value;
                if (_familiesView.View != null)
                    _familiesView.View.Filter = x =>
                        {
                            if (string.IsNullOrEmpty(_filter))
                                return true;
                            FamilyDto fam = x as FamilyDto;
                            if (fam == null)
                                return false;
                            return fam.Name.ToLower().Contains(_filter.ToLower());

                        };
                RaisePropertyChanged("Filter");
            }
        }


        private ObservableObjectCollection _families;
        public ObservableObjectCollection Families
        {
            get
            {
                return _families;
            }
            set
            {
                _families = value;
                FamiliesView.Source = value;
                RaisePropertyChanged("Families");
            }
        }

        private CollectionViewSource _familiesView = new CollectionViewSource();
        public CollectionViewSource FamiliesView
        {
            get
            {
                return _familiesView;
            }
            set
            {
                _familiesView = value;

                RaisePropertyChanged("FamiliesView");
            }
        }


        private FamilyDto _selectedFamily;
        public FamilyDto SelectedFamily
        {
            get
            {
                return _selectedFamily;
            }
            set
            {
                _selectedFamily = value;

                RaisePropertyChanged("SelectedFamily");
            }
        }

        private FamilyDto _familyToMove;
        public FamilyDto FamilyToMove
        {
            get
            {
                return _familyToMove;
            }
            set
            {
                _familyToMove = value;
                _moveMode = MoveMode.Family;
                RaisePropertyChanged("FamilyToMove");
            }
        }


        private List<GeneDto> _genesToMove;
        public List<GeneDto> GenesToMove
        {
            get
            {
                return _genesToMove;
            }
            set
            {
                _genesToMove = value;
                _moveMode = MoveMode.Genes;
                RaisePropertyChanged("GenesToMove");
            }
        }





        IDataService _dataService;
        INavigationService _navigationService;
        public MoveToFamilyViewModel(IDataService dataService, INavigationService navigationService)
        {


            _dataService = dataService;
            _navigationService = navigationService;
            _familiesView.SortDescriptions.Add(new System.ComponentModel.SortDescription("Name", System.ComponentModel.ListSortDirection.Ascending));
            if (IsInDesignMode)
            {
                _dataService.RefreshFamiliesAsync(op =>
                    {
                        Families = new ObservableObjectCollection(op.Entities);

                    });
            }
        }



        private RelayCommand _load;
        public RelayCommand Load
        {
            get
            {
                return _load ?? (_load = new RelayCommand(ExecLoad, CanLoad));
            }
        }

        /// <summary>
        /// Checks whether the Load command is executable
        /// </summary>
        private bool CanLoad()
        {
            return true;
        }

        /// <summary>
        /// Executes the Load command 
        /// </summary>
        private void ExecLoad()
        {
            Filter = null;
            this.Families = new ObservableObjectCollection(_dataService.Families);
        }




        private RelayCommand _move;
        public RelayCommand Move
        {
            get
            {
                return _move ?? (_move = new RelayCommand(ExecMove, CanMove));
            }
        }

        /// <summary>
        /// Checks whether the Move command is executable
        /// </summary>
        private bool CanMove()
        {
            return true;
        }

        /// <summary>
        /// Executes the Move command 
        /// </summary>
        private void ExecMove()
        {

            base.ReportProgress();

            switch (_moveMode)
            {
                case MoveMode.Family:
                    {
                        _familyToMove.ParentId = SelectedFamily.Id;
                        break;
                    }
                case MoveMode.Genes:
                    {
                        foreach (var gene in _genesToMove)
                        {
                            gene.FamilyId = _selectedFamily.Id;
                        }
                        break;

                    }

            }



            _dataService.SubmitAsync(op =>
                {
                    base.FinishProgress();
                    if (op.HasError)
                    {
                        base.HandleDomainError(op, "Moving to family failed.");
                    }
                    else
                    {

                    }
                    _navigationService.CloseMoveToFamily();
                });
        }


        private enum MoveMode
        {
            Family,
            Genes
        }
    }
}
